<template>
  <div v-if="imgs.length && !isAddingAnnotation">
    <div
      class="qts-img"
      :class="{ grabbing: isDragging }"
      @pointerdown="onPointerDown"
      @pointermove="onPointerMove"
      @pointerup="onPointerUp"
    >
      <div class="view-wrapper" :style="styleStr">
        <div class="img-container">
          <img
            :draggable="false"
            class="paper-img"
            :src="imgs[imgIndex]"
            alt=""
            :style="{ transform: isRotated ? 'rotate(180deg)' : 'rotate(0)' }"
          />
        </div>
      </div>
    </div>
    <div class="flex-end tool-box">
      <el-button
        type="primary"
        text
        class="annotation-btn"
        @click="onToggleMark"
      >
        <AnnotationBtn class="img" />
        批注
      </el-button>
      <div class="control-btn flex">
        <div
          class="btn btn-left hover"
          @click="onScroll('left')"
          :class="imgIndex === 0 && 'btn-disable'"
        ></div>
        {{ imgIndex + 1 }} / {{ imgs.length }}
        <div
          class="btn btn-right hover"
          :class="
            (imgs.length === 1 || imgIndex === imgs.length - 1) && 'btn-disable'
          "
          @click="onScroll('right')"
        ></div>
      </div>
    </div>
  </div>
  <template v-else-if="imgs.length">
    <AnnotationBox :img-url="imgs[imgIndex]" />
    <AnnotationControl />
  </template>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { annotateMitt } from '@/utils/tools/bus'
import AnnotationBtn from '@/assets/images/assignment/annotation.svg'
import AnnotationBox from '@/components/annotation/AnnotationBox.vue'
import AnnotationControl from '@/components/annotation/AnnotationControl.vue'
import { useSingleQtsCorrectStore } from '@/stores/single-correct'
import { storeToRefs } from 'pinia'

const { annotation_url } = storeToRefs(useSingleQtsCorrectStore())
const props = defineProps({
  imgUrl: { type: String, required: true },
  scaleRatio: { type: Number, default: 1 },
  isRotated: { type: Boolean, default: false }
})

const isDragging = ref(false)
const translateX = ref(0)
const translateY = ref(0)

const styleStr = computed(() => {
  return `transform:  translate(${translateX.value}px,${translateY.value}px) scale(${props.scaleRatio})`
})

const imgs = ref([])
const imgIndex = defineModel('imgIndex', {
  default: 0
})
watch(
  () => props.imgUrl,
  () => {
    imgs.value = JSON.parse(props.imgUrl)
  },
  {
    immediate: true
  }
)

const onScroll = type => {
  if (type === 'left') {
    imgIndex.value = imgIndex.value === 0 ? 0 : imgIndex.value - 1
    return
  }
  imgIndex.value =
    imgIndex.value === imgs.value.length - 1
      ? imgIndex.value
      : imgIndex.value + 1
}

const onPointerDown = () => {
  isDragging.value = true
}

const onPointerUp = () => {
  isDragging.value = false
}

const onPointerMove = e => {
  if (!isDragging.value) {
    return
  }
  const { movementY, movementX } = e
  // 给点击事件一个手滑的缓冲区
  if (Math.abs(movementY) < 3 && Math.abs(movementX) < 3) {
    return
  }

  if (props.scaleRatio > 1) {
    translateY.value += movementY
    translateX.value += movementX
  }
}

// ==== start 批注相关
const isAddingAnnotation = ref(false) // 是否开启批注
const onToggleMark = () => {
  isAddingAnnotation.value = !isAddingAnnotation.value
}
const annotateMittListener = () => {
  annotateMitt.on('annotationControl', param => {
    let { model } = param
    switch (model) {
      case 'cancel':
        isAddingAnnotation.value = false
        break
      case 'save-img-url':
        isAddingAnnotation.value = false
        imgs.value[imgIndex.value] = param.url
        break
    }
  })
}

watch(
  () => imgs.value,
  () => {
    annotation_url.value = imgs.value.filter(url =>
      url.includes('web_annotation_')
    )
  },
  {
    deep: true
  }
)

// ==== end 批注相关

watch(
  () => props.scaleRatio,
  val => {
    if (val == 1) {
      translateY.value = 0
      translateX.value = 0
    }
  }
)

const initData = () => {
  annotateMittListener()
}
initData()
</script>

<style lang="less" scoped>
@baseURL: '@/assets/images/single-correct';
.qts-img {
  position: relative;
  overflow: hidden;
  .view-wrapper {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transform-origin: top;
    max-height: calc(100vh - 220px);
    overflow: scroll;
    .img-container {
      width: 100%;
      position: relative;
      user-select: none;
      .paper-img {
        display: block;
        border: 1px solid #d9d9d9;
        box-sizing: border-box;
        width: 100%;
        background-color: rgba(232, 232, 232, 0.5);
        background-image: url('@{baseURL}/loading.svg');
        background-repeat: no-repeat;
        background-position: center;
        transition: all 200ms;
      }
    }
  }
}
.grabbing {
  cursor: grabbing;
}
.tool-box {
  .annotation-btn {
    gap: 20px;
    .img {
      margin-top: -2px;
      margin-right: 4px;
    }
  }
  .control-btn {
    gap: 30px;
    .btn {
      width: 30px;
      height: 30px;
      margin-bottom: -4px;
    }
    .btn-disable:hover {
      cursor: not-allowed;
    }
    .btn-left {
      background-image: url('@{baseURL}/left-btn.svg');
      background-repeat: no-repeat;
      background-size: contain;
    }
    .btn-right {
      background-image: url('@{baseURL}/right-btn.svg');
      background-size: contain;
      background-repeat: no-repeat;
    }
  }
}
</style>
